const app = getApp()
Page({
  data: {
    id: 0,
    name: '',
    hidden: true,
    inputValue: ''
  },
  onLoad(obj) {
    this.setData({
      id: obj.id,
      name: obj.name
    })
  },
  edit() {
    this.setData({
      hidden: !this.data.hidden
    })
  },
  modalConfirm() {
    let {
      tags
    } = app.data
    const tag = tags.filter(item => item.name === this.data.name)[0]
    const {
      inputValue
    } = this.data
    if (!inputValue) {
      return '';
    } else {
      this.setData({
        name: inputValue
      })
      tag.name = inputValue
      this.setData({
        tags
      })
      wx.setStorageSync('tags', tags);
    }
    this.setData({
      hidden: !this.data.hidden
    })
    wx.navigateBack({
      delta: 1
    })
  },
  modalCancel() {
    this.setData({
      hidden: !this.data.hidden
    })
  },
  bindKeyInput(e) {
    const {
      value
    } = e.detail
    this.setData({
      inputValue: value
    })
  },
  delete() {
    const {
      tags
    } = app.data
    const {
      name
    } = this.data
    // const tag = tags.filter(i => i.name === name)[0]
    // console.log(tag.id);
    let index = -1
    for (let i = 0; i < tags.length; i++) {
      if (tags[i].name === name) {
        index = i
        break;
      }
    }
    if (index >= 0) {
      tags.splice(index, 1)
    }
    wx.setStorageSync('tags', tags);
    wx.navigateBack({
      delta: 1
    })
  },
})